<template>
  <div>
    <el-input v-model="searchText" placeholder="请输入推广title或description" width="50"></el-input>
    <el-button @click="fetchList()">搜索</el-button>
    <el-button class="mb-3" type="primary" @click="addTagDialogVisible = true">添加推广</el-button>
    <el-dialog :visible.sync="addTagDialogVisible" title="添加推广">
      <el-form :model="addTagForm" label-width="80px">
        <el-form-item label="推广title">
          <el-input v-model="addTagForm.title"></el-input>
        </el-form-item>
        <el-form-item label="推广link">
          <el-input v-model="addTagForm.link"></el-input>
        </el-form-item>
        <el-form-item label="推广description">
          <el-input v-model="addTagForm.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addTagDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addPromote">添加</el-button>
      </div>
    </el-dialog>
    <el-table :data="tags" style="width: 100%">
      <el-table-column label="推广id" prop="id"></el-table-column>
      <el-table-column label="推广title" prop="title"></el-table-column>
      <el-table-column label="推广link" prop="link"></el-table-column>
      <el-table-column label="description" prop="description"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button round type="warning" @click="editTag(scope.row)">编辑</el-button>
          <el-button round type="danger" @click="deletePromote(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="editTagDialogVisible" title="编辑推广">
      <el-form :model="editTagForm" label-width="80px">
        <el-form-item label="推广title">
          <el-input v-model="editTagForm.title"></el-input>
        </el-form-item>
        <el-form-item label="推广link">
          <el-input v-model="editTagForm.link"></el-input>
        </el-form-item>
        <el-form-item label="description">
          <el-input v-model="editTagForm.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editTagDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="updatePromote">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getPromotion, addPromote, updatePromote, deletePromote} from '@/api/promote'

export default {
  name: 'PromotionList',
  data() {
    return {
      tags: [],
      searchText: "",
      addTagDialogVisible: false,
      addTagForm: {
        title: '',
        link: '',
        description: ''
      },
      editTagDialogVisible: false,
      editTagForm: {
        id: '',
        title: '',
        link: '',
        description: ''
      }
    }
  },
  created() {
    this.fetchList()
  },
  methods: {
    fetchList() {
      getPromotion(decodeURIComponent(this.searchText)).then(response => {
        this.tags = response.data
      })
    },
    addPromote() {
      addPromote(this.addTagForm).then(() => {
        this.addTagDialogVisible = false
        this.fetchList()
      })
    },
    editTag(row) {
      this.editTagForm.id = row.id
      this.editTagForm.title = row.title
      this.editTagForm.link = row.link
      this.editTagForm.description = row.description
      this.editTagDialogVisible = true
    },
    updatePromote() {
      // console.log(this.editTagForm);
      updatePromote(this.editTagForm).then(() => {
        this.editTagDialogVisible = false
        this.fetchList()
      })
    },
    deletePromote(row) {
      this.$confirm(`确认删除标签 ${row.name} 吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletePromote(row.id).then(() => {
          this.fetchList()
        })
      })
    }
  }
}
</script>